package com.zh.baselibrary.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.WindowManager;

import androidx.annotation.Nullable;

import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import butterknife.ButterKnife;

public abstract class CountTimeBaseActivity<P extends BasePresenter>extends RxAppCompatActivity implements BaseView{
    /**
     * 空闲时间
     */
    private static final Long MILLISINFUTURE = (long) 61 * 1000;
    /**
     * 倒计时间隔
     */
    private static final Long COUNTDOWNINTERVAL = (long) 1 * 1000;
    public static final Integer RESULT_CODE_TIME_FINISH = 1001;
    public CountDownTimer countTimer;
    protected P mPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        if(getLayoutId()!=0)
            setContentView(getLayoutId());
        ButterKnife.bind(this);
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachModelView( this, this);
        }
        init();
        initCountTimer();
    }
    protected abstract int getLayoutId();
    protected abstract void init();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_CODE_TIME_FINISH) {
            timeFinish();
        }
    }
    /**
     * 使用MVP模式时需要重写该方法
     */
    protected abstract  P createPresenter();
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDetach();
        }
    }
    private void initCountTimer() {
        countTimer = new CountDownTimer(MILLISINFUTURE, COUNTDOWNINTERVAL) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeTick(millisUntilFinished);
            }

            @Override
            public void onFinish() {
               timeFinish();
            }
        };
    }
    public void timeFinish() {

    }


    public void timeTick(Long millisUntilFinished) {

    }
    public void timeStart() {
        new Handler(this.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                countTimer.start();
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        timeStart();
    }
    @Override
    public void onPause() {
        super.onPause();
        if (countTimer != null) {
            countTimer.cancel();
        }
    }
    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return bindUntilEvent(ActivityEvent.STOP);
    }

    @Override
    public void onSuccess(Object object) {

    }

    @Override
    public void onHint(String hint) {
    }
}
